<?php
/*
 * @Author: 逅来
 * @Description: 
 * @Date: 2023-03-20 09:29:05
 * @LastEditTime: 2023-03-28 20:00:10
 * @FilePath: \zhaopian.com\application\api\controller\photo\User.php
 * @email: 934749708@qq.com
 */
/*
 * @Author: 逅来
 * @Description: 
 * @Date: 2023-03-20 09:29:05
 * @LastEditTime: 2023-03-20 11:06:22
 * @FilePath: \xiangce_houtai-master\application\api\controller\photo\Itinerary.php
 * @email: 934749708@qq.com
 */
/*
 * @Author: 逅来
 * @Description: 
 * @Date: 2023-03-19 14:55:20
 * @LastEditTime: 2023-03-20 09:51:17
 * @FilePath: \xiangce_houtai-master\application\api\controller\photo\Itinerary.php
 * @email: 934749708@qq.com
 */



namespace app\api\controller\photo;

use app\common\controller\Api;

use app\admin\model\photo\User as UserModel;
use app\admin\model\photo\Itinerary as ItineraryModel;
use app\admin\model\photo\Bind as BindModel;
use app\admin\model\photo\Related as RelatedModel;
use app\admin\model\photo\Route as RouteModel;
use app\admin\model\photo\Phone as PhoneModel;
use app\admin\model\photo\Information as InformationModel;

use app\api\controller\Validate;
use Think\Db;
class User extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    public function _initialize()
    {
        // $data = $this->request->param('');
        // $user = UserModel::where('id', $data['user_id'])->where('token',$data['token'])->where('Expiration_time','> time',time())->find();
        // if(!$user){
        //     json(array('code' => 200, 'msg' => 'token过期重新登录', 'type' => 0))->send();
        //     exit();
        // }
        // parent::_initialize(); 
        
    }
    public function succ_ess($number, $text, $title, $list, $data)
    {
        return json(['code' => $number, 'msg' => $text, $title => $list, 'data' => $data]);
    }
    /**
     * @description: 游客端上传个人信息
     * @return {*}
     * @author: 逅来
     */    
    public function UserInfo(){
      
        $data = $this->request->param('');
        $Validate = new Validate();
        $Validate->UserInfo($data);
        $user = UserModel::get($data['user_id']);
        $user->nickname = $data['nickname'];
        $user->headerimg = $data['headerimg'];
        $user->name = $data['name'];
        $user->age = $data['age'];
        $user->gender = $data['gender'];
        $user->phone = $data['phone'];
        $user->region = $data['region'];
        $user->address = $data['address'];
        $res= $user->save();
        if($res){
            return $this->succ_ess(200, '游客个人信息上传成功', 'type', 1, '');
        }
    }
    /**
     * @description: 游客端个人信息
     * @return {*}
     * @author: 逅来
     */    
    public function GetUserInfo(){
      
        $data = $this->request->param('');
        $user = UserModel::get($data['user_id']);
        if($user){
            return $this->succ_ess(200, '游客个人信息详情', 'type', 1, $user);
        }
    }
    // 游客 我的行程
    public function myItinerary(){
         $data = $this->request->param('');
        $list = Db::table('fa_photo_bind')
        ->alias('a')
         ->join('fa_photo_itinerary b', 'b.id = a.itinerary_id','LEFT')
        ->where('a.user_id',$data['user_id'])
        ->select();
        foreach ($list as $k => &$v){
            $v['route'] = Db::table('fa_photo_route')->where('id',$v['route_id'])->find();
            $v['count'] = RelatedModel::where('user_id',$data['user_id'])->where('itinerary_id',$v['itinerary_id'])->count();
        }    
         foreach ($list as $k => &$v){
           $v['route'] = Db::table('fa_photo_city')->whereIn('id', $v['route']['city_id'])->field('name')->select();
        }    
      
        return $this->succ_ess(200, '游客 我的行程', 'type', 1, $list );
    }
     //游客行程上传图片
     public function update(){
           $data = $this->request->param('');
            $Validate = new Validate();
            $Validate->update($data);
            $file_id = explode(',',$data['file_id']);
            foreach ($file_id as $key => $value) {
                $file_id[$key] = array("file_id" => $value);
            }
             foreach ($file_id as $key => &$v) {
                 $v['itinerary_id'] = $data['itinerary_id'];
                 $v['city_id'] = $data['city_id'];
                 $v['user_id'] = $data['user_id'];
             
            }
              $RelatedModel = new RelatedModel();
              $res = $RelatedModel->saveAll($file_id);
            if($res){
                // 行程更改为已开始
                $Itinerary = ItineraryModel::get($data['itinerary_id']);
                $Itinerary->status = 1;
                $Itinerary->save();
            return $this->succ_ess(200, '提交成功', 'type', 1, '');
        }
     }
    // 行程详情
    public function Detailed(){
        $data = $this->request->param('');
       $Itinerary = ItineraryModel::get($data['id']);  
       $Itinerary['number_count'] =RelatedModel::where('itinerary_id',$data['id'])->group('user_id')->count();
        // $Itinerary['file_count'] =RelatedModel::where('itinerary_id',$data['id'])->group('user_id')->count();
       $route = RouteModel::get($Itinerary->route_id);
        $city = Db::table('fa_photo_city')->whereIn('id', $route->city_id)->field('id,name')->select();
        $length = 0;
        foreach ($city as $k => &$v){
            $v['file'] = Db::table('fa_photo_related')
                        ->alias('a')
                        ->join('fa_photo_file b', 'b.id = a.file_id','LEFT')
                        ->where('a.itinerary_id',$data['id'])
                        ->where('a.city_id', $v['id'])
                        ->where('a.user_id', $data['user_id'])
                        ->field('a.id,b.fullurl as url')
                        ->select(); 
                        $length += count($v['file']);
 
        }
      
       $Itinerary['file_count'] = $length;
        $res['Itinerary'] = $Itinerary;
        $res['city'] = $city;
       
        return $this->succ_ess(200, '行程详情', 'type', 1, $res);
    }

    // 判断身份
    public function identity1(){
        $data = $this->request->param('');
        $list = PhoneModel::where('phone',$data['phone'])->find();
        if($list){
           $status=2;
        }else{
             $status=1;
        }
        $user = UserModel::get($data['user_id']);
        $user->phone = $data['phone'];
        $user->status = $status;
        $user->save();
        $res['user_id'] = $data['user_id'];
        $res['identity'] = $user->status;
         return $this->succ_ess(200, '判断身份', 'type', 1, $res);
    }
    
    // 判断身份
    public function identity(){
        $data = $this->request->param('');
        $list = PhoneModel::where('phone',$data['phone'])->find();
        if($list){
           $status=2;
        }else{
             $status=1;
        }
        $user = UserModel::get($data['user_id']);
        $user->nickname = $data['nickname'];
        $user->headerimg = $data['headerimg'];
        $user->status = $status;
        $user->save();
        $res['user_id'] = $data['user_id'];
        $res['identity'] = $user->status;
         return $this->succ_ess(200, '判断身份', 'type', 1, $res);
    }
   
    // 删除行程的图片
    public function DelFile(){
         $data = $this->request->param('');
        
         $RelatedModel = RelatedModel::where('file_id',$data['id']);
         $res = $RelatedModel->delete();
         if($res){
             return $this->succ_ess(200, '删除图片成功', 'type', 1, $res);
         }
    }
     // 批量删除行程的图片
    public function DelFileAll(){
        $data = $this->request->param('');
       
        $RelatedModel = RelatedModel::whereIn('file_id',$data['id']);
        $res = $RelatedModel->delete();
        if($res){
            return $this->succ_ess(200, '删除图片成功', 'type', 1, $res);
        }
   }
    // 退出登录
    public function LogOut(){
         $data = $this->request->param('');
         $user = UserModel::get($data['user_id']);
         $user->token = '';
         $res = $user->save();
         if($res){
              return $this->succ_ess(200, '退出登录成功', 'type', 1, '');
         }
    }
 
}
